<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Vip; // 引入Vip模型

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('email');
            // $grid->column('pass');
            // $grid->column('name');
            $grid->column('status')->using([1 => '正常', 0 => '封禁'])->label([
                // 'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                // 1 => 'primary',
                0 => 'danger',
                1 => 'success',
            
            ]);
            $vips = Vip::pluck('name', 'id')->toArray();
            $grid->column('vip_id')->using($vips)->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                // 1 => 'primary',
                // 0 => 'danger',
                // 1 => 'success',
            
            ]);;
            // $grid->column('vip_expire');
            $grid->column('balance');
            $grid->column('forward');
            $grid->column('dwz');
            $grid->column('created_at');
            // $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('email');
            $show->field('pass');
            $show->field('name');
            $show->field('status');
            $show->field('vip_id');
            $show->field('vip_expire');
            $show->field('balance');
            $show->field('forward');
            $show->field('dwz');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('email');
            $form->text('pass');
            $form->text('name');
            $form->select('status')->options(['0' => '关闭','1' => '开启',]);
            
            // 修改vip_id字段为下拉选择框，并显示vip表中的name
            $vips = Vip::pluck('name', 'id')->toArray();
            $form->select('vip_id')->options($vips);
            
            $form->datetime('vip_expire');
            $form->text('balance');
            $form->text('forward');
            $form->text('dwz');
        
            // $form->display('created_at');
            // $form->display('updated_at');
        });
    }
}
